package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap;

import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.common.OtwConstant;
import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.dto.DeliveryOrder;
import com.alibaba.cirtus.ots.application.adapter.ability.model.Result;
import com.alibaba.cirtus.ots.application.adapter.ability.model.enums.MnSAPStatus;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpTokenUtil;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpUtils;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import com.epoch.app.otsapplicationadaptersdk.mnsapif043v2.service.MnSapIf043V2Service;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnSapIf043Request;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnSapIf043SDO;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommResponse;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommSDO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import tools.Json;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * SAP 交货单更改、删除 call back接口F-B、B-b
 * SAP->中台
 * @author baizhuo
 * @version 1.0
 * @date 2024/1/8 21:06
 */
@Component
@Primary
public class MnSapIf043V2ServiceImpl implements MnSapIf043V2Service {

    private final Log log = Log.getLogger(MnSapIf043V2ServiceImpl.class);

    @Autowired
    private HttpUtils httpUtils;

    @Override
    @ProcessInvoker(remark = "MnSapIf043V2Service", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public MnSapCommResponse deliveryOrderUpdateDeleteCallback(MnSapIf043Request mnSapIf043Request) {
        log.info("MnSapIf043V2Service deliveryOrderUpdateDeleteCallback mnSapIf043Request:" + JSONObject.toJSONString(mnSapIf043Request));
        MnSapCommResponse mnSapCommResponse = new MnSapCommResponse();
        mnSapCommResponse.setInterfacePath(mnSapIf043Request.getMessageHeader().getINTERFACE());
        mnSapCommResponse.setMessageId(mnSapIf043Request.getMessageHeader().getMESSAGEID());
        List<MnSapCommSDO> mnSapCommSDOS = new ArrayList<>();
        MnSapCommSDO mnSapCommSDO = new MnSapCommSDO();
        try {
            List<DeliveryOrder> deliveryOrderList = new ArrayList<>();
            for (MnSapIf043SDO mnSapIf043SDO : mnSapIf043Request.getITEM1()) {
                DeliveryOrder deliveryOrder = new DeliveryOrder();
                deliveryOrder.setSapDeliveryOrder(mnSapIf043SDO.getVBELN_VL());
                deliveryOrder.setFlag(mnSapIf043SDO.getFLAG());
                deliveryOrder.setMessage(mnSapIf043SDO.getMESSAGE());
                deliveryOrder.setRoute(mnSapIf043SDO.getROUTE());
                deliveryOrderList.add(deliveryOrder);
            }
            JSONObject dataJSON = new JSONObject();
            dataJSON.put("deliveryOrderList", deliveryOrderList);
            Map<String, String> header = HttpTokenUtil.generateHttpHeader();
            String urlPrefix = OtsApplicationAdapterSdkConstants.urlPrefix();
            String url = urlPrefix + "api/mn_bc_link/deliveryOrderUpdateDeleteCallBack";
            log.info("MnSapIf043V2Service deliveryOrderUpdateDeleteCallback call bc link interface,url:{}, header:{}, param:{}", url, header, JSON.toJSONString(dataJSON));
            ResponseEntity<Result> post = httpUtils.post(url, header, dataJSON, Result.class);
            Result result = post.getBody();
            log.info("MnSapIf043V2Service deliveryOrderUpdateDeleteCallback result:{}", JSON.toJSONString(result));
            if (result.isSuccess()) {
                mnSapCommSDO.setStatus(MnSAPStatus.SUCCESS.getCode());
            } else {
                mnSapCommSDO.setStatus(MnSAPStatus.ERROR.getCode());
                mnSapCommSDO.setErrCode(result.getErrorCode());
                mnSapCommSDO.setMessage(result.getMessage());
            }
        } catch (Exception e) {
            log.error("call bc-link deliveryOrderUpdateDeleteCallback error", e);
            mnSapCommSDO.setStatus(MnSAPStatus.ERROR.getCode());
            mnSapCommSDO.setErrCode(OtwConstant.ORDER_001_01_01_001);
            mnSapCommSDO.setMessage(MnSAPStatus.ERROR.getMsg());
        }
        mnSapCommResponse.setData(Lists.newArrayList(mnSapCommSDO));
        return mnSapCommResponse;
    }
}
